<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../lib/layui/layui.all.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <script type="text/javascript" src="../../js/commonutil.js"></script>
    <script src="./function.js" charset="utf-8"></script>
</head>
<body>
<div class="edit-form-class" style="padding-top: 20px;">
    <form class="layui-form" id="addTemplateForm" onsubmit="return false">
        <div class="layui-form-item">
            <div class="layui-row">
                <label class="layui-form-label">*模板编号</label>
                <div class="layui-input-inline" style="padding-top: 8px;width: 135px">
                    <span id="templateNoPrefix" style="color:#FF0000">请选择产品</span>
                </div>
                <div class="layui-input-inline">
                    <input type="text" id="templateNo" name="templateNo"
                           lay-verify="required|templateNoVerify"
                           style="width: 206px" autocomplete="off" placeholder="" maxlength="50" class="layui-input">
                </div>
            </div>
<!--            <label class="layui-form-label"><span style="color:red">*</span>模板编号</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" id="templateNo" name="templateNo" lay-verify="required|templateNoVerify" maxlength="50"-->
<!--                       style="width: 350px" autocomplete="off" placeholder="" class="layui-input">-->
<!--            </div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>模板名称</label>
            <div class="layui-input-block">
                <input type="text" id="templateName" name="templateName" lay-verify="required|templateNameVerify" maxlength="50"
                       style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>模板来源</label>
            <div class="layui-input-block">
                <input type="radio" name="templateResource" lay-filter="templateListen" value="1" title="附件上传" checked>
                <input type="radio" name="templateResource" lay-filter="templateListen" value="2" title="线上编辑">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>模板文件</label>
            <div class="layui-input-block" style="padding-top: 5px">
                <a url = "" id="fileName" >请上传文件</a>
                <button type="button" class="layui-btn layui-btn-normal" id="uploadFile">
                    上传文件
                </button><span style="color:#999">只能上传ftl格式文件，文件不能超过100kb</span>
                <input id='templateExtId' name='templateExtId' type="text" class="text" maxlength='50'
                       style='display:none;'/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>模板文件</label>
            <div class="layui-input-block" style="padding-top: 5px">
                <textarea id="templateEdit" name="templateEdit" style="display: none;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>产品</label>
            <div class="layui-input-block" style="width: 350px">
                <select name="channelId" lay-filter="channelId" id="channelId" lay-verify="required|sealTypeVerify">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>签章类型</label>
            <div class="layui-input-block" style="width: 350px">
                <select name="sealType" lay-filter="sealType" id="sealType" lay-verify="required|sealTypeVerify">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>公章规则</label>
            <div class="layui-input-block" id="officialSealId" style="width: 350px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>配置审批流</label>
            <div class="layui-input-block"  id = "approval"> </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <textarea placeholder="请输入内容" maxlength="200" name="remark" id="remark" style="width: 400px" class="layui-textarea" lay-verify="remarksVerify"></textarea>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" autocomplete="off" lay-filter="submitBtn" id="submitBtn">提交申请
                </button>
                <button type="reset" id="closeNow" class="layui-btn layui-btn-primary">取消</button>
            </div>
        </div>
    </form>
</div>
</body>

<script>

    var index;
    jQuery(function () {
        layui.use(['laypage', 'jquery', 'layer', 'table', 'form', 'util', 'upload', 'layedit'], function () {
            let form = layui.form;
            let upload = layui.upload;
            let layedit = layui.layedit;
            //建立编辑器
            index = layedit.build('templateEdit');
            //隐藏文本编辑器
            hideLayEdit(form);
            //初始化渠道
            queryChannels(form);
            //初始化签章类型
            querySealType(form);
            //radio监听
            radioListener(form);
            //select监听
            selectListener(form);
            //文件上传
            let uploadUrl = serverPre + "/manager/scs/v2/template/upload";
            uploadFile(upload, uploadUrl);
            form.render();
        });
    });
    $("#closeNow").click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })
    //提交
    $('#submitBtn').click(function () {
        var approverObj=approval.result();
        debugger;
        console.log(approverObj)
        let url = serverPre + "/manager/scs/v2/template/save/ext";
        let formData = serializeObject($("#addTemplateForm"));
        let officialSealId = [];
        $("#officialSealId").find('input:checkbox').each(function (i, item) {
            if ($(this).is(':checked')) {
                officialSealId.push(item.value);
            }
        });
        layui.use(['jquery', 'layedit'], function () {
            let layedit = layui.layedit;
            let fileData = layedit.getText(index);
            /*//
            let channelId = "HYD201900001";*/
            //公章规则号校验
            if (formData.sealType !== "1") {
                if (officialSealId.length === 0) {
                   // alert("公章规则号不能为空！");
                    return false;
                }
            }
            //是否上传模板校验
            debugger;
            if (formData.templateExtId === "" && fileData === "") {
                alert("请上传模板！");
                return false ;
            }
            let commitData = JSON.stringify({
                "templateNo": $('#templateNoPrefix').html()+formData.templateNo,
                "templateName": formData.templateName,
                "fileData": fileData,
                "refId": formData.templateExtId,
                "resource": formData.templateResource,
                "sealType": formData.sealType,
                "channelNo": formData.channelId,
                "officialSealId": officialSealId,
                "userInfoRos": approverObj.userInfos,
                "approverIds": approverObj.approvers,
                "remark": formData.remark
            });
            submitForm(commitData, url);
        })
    });
    $(function () {
        $("#approval").load("../approval/approvalConfig.html","",function () {
            approval = initSubmitApproval("开始");
            //initApprovalByDatas(initApproversByData(getApprovalDetail()));
        });

       /* function initApproversByData(approvalDetail) {
            var approvers=[{name:approvalDetail["applyUserName"],status:1,isApplyUser:true}];
            approvalDetail.approvers.forEach(function (e) {
                approvers.push({userId:e.approverId,userName: e.approverName,name:e.approverName,status:e.approvalStatus});
            })
            return approvers;
        }*/
    })

    function getApprovalDetail(approvalId) {
        var data = null;
        $.ajax({
            type: 'get',
            url: serverPre1 + "/manager/saas/v1/approval/detail/" + approvalId,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: false,
            cache: false,
            success: function (result) {
                if (result.code == 0 && result.data) {
                    data = result.data
                }
            }
        });
        return data;
    }


</script>
</html>
